﻿package com.projects.xenia.documents_classes;


import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import com.projects.xenia.global_modules.MainGlobalModule;

import android.content.ContentValues;
import android.database.Cursor;



public class Documents_Payment {

	public DocHeader Head;
	public DocLine Line;
	
	
	
	public boolean BackupIsExists () {
		
		Cursor _result = MainGlobalModule.DBE.rawQuery("select id from sxdoc_payments_h where id = ?", new String [] {Head.id});
		return (_result.getCount()>0);
		
	}
	
	public void RestoreDocument () {
	
		MainGlobalModule.DBE.beginTransaction();
		
		MainGlobalModule.DBE.delete("xdoc_payments_h", "id = ?", new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_payments_h select * from sxdoc_payments_h where id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("xdoc_payments_t", "hdoc_id = ?",  new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_payments_t select * from sxdoc_payments_t where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("xdoc_payments_d", "hdoc_id = ?",  new String [] {Head.id});
		MainGlobalModule.DBE.execSQL("insert into xdoc_payments_d select * from sxdoc_payments_d where hdoc_id = ?", new String [] {Head.id});		
			
		MainGlobalModule.DBE.setTransactionSuccessful();
		MainGlobalModule.DBE.endTransaction();
		
	}
	
	public void BackupDocument () {
		
		MainGlobalModule.DBE.beginTransaction();
		
		MainGlobalModule.DBE.delete("sxdoc_payments_h", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_payments_h select * from xdoc_payments_h where id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("sxdoc_payments_t", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_payments_t select * from xdoc_payments_t where hdoc_id = ?", new String [] {Head.id});
		
		MainGlobalModule.DBE.delete("sxdoc_payments_d", null, null);
		MainGlobalModule.DBE.execSQL("insert into sxdoc_payments_d select * from xdoc_payments_d where hdoc_id = ?", new String [] {Head.id});		
			
		MainGlobalModule.DBE.setTransactionSuccessful();
		MainGlobalModule.DBE.endTransaction();
		
	}
	
	
	public class DocHeader {
		
		public String id;
		public int docnum;
		public Date date_create;
		public Date date_save;		
		public int state;
		public int verstamp;
		public String acctype;
		public String firms_id;		
		public String user_id;
		public String employees_id;
		public String customers_id;
		public int planned;
		public double total_paymentsum;		
			
	}
	
	public class DocLine {
		
		public String id;
		public String hdoc_id;		
		public String ddoc_id;
		public String dtypedoc_id;
		public String dtypedoc_descr;
		public String ddocnum ;
		public Date ddocdate;
		public double ddorder_sum;
		public double payment_sum;
		
	}
	
	private int GetLastDocNumByCurrentDate () {
		
		int DocNum = 0;
		Cursor ScalarResult = MainGlobalModule.DBE.rawQuery("select max(docnum) from xdoc_Payments_h", null);
		
		if (ScalarResult.getCount()>0) {
			ScalarResult.moveToFirst();
			DocNum = ScalarResult.getInt(0);								
		}
		
		ScalarResult.close();
		
		return (DocNum+1);
		
	}	
	
	public boolean OpenDoc (String Id) {

		Head = new DocHeader ();
		Line = new DocLine ();
		
		try {
			
			Cursor DocumentsResult = MainGlobalModule.DBE.rawQuery("select * from xdoc_Payments_h where id = ?",new String [] {Id});
		
			DocumentsResult.moveToFirst();
			
			Head.id = DocumentsResult.getString(DocumentsResult.getColumnIndex("id"));
			Head.docnum = DocumentsResult.getInt(DocumentsResult.getColumnIndex("docnum"));		
			Head.date_create = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_create"))); 
			Head.date_save = MainGlobalModule.SqlStringToDate(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_save")));
			Head.state = DocumentsResult.getInt(DocumentsResult.getColumnIndex("state"));		
			Head.verstamp = DocumentsResult.getInt(DocumentsResult.getColumnIndex("verstamp"));
			Head.acctype = DocumentsResult.getString(DocumentsResult.getColumnIndex("acctype")); 
			Head.firms_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("firms_id"));					
			Head.user_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("user_id"));
			Head.employees_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("employees_id"));		
			Head.customers_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("customers_id"));		
			Head.planned = DocumentsResult.getInt(DocumentsResult.getColumnIndex("planned"));			
			Head.total_paymentsum = DocumentsResult.getDouble(DocumentsResult.getColumnIndex("total_paymentsum"));
		
			BackupDocument (); 
			
		} catch (Exception e) {
			
			return false;
			
		} 
		
		
		
		return true;	
		
	}	
	
	public boolean NewDoc () {
		
		Head = new DocHeader ();
		Line = new DocLine ();
		
		try {
			
			Head.id = UUID.randomUUID().toString().toUpperCase();
			Head.docnum = GetLastDocNumByCurrentDate ();			
			Head.date_create = Calendar.getInstance().getTime();
			Head.date_save = Calendar.getInstance().getTime();			
			Head.state = 1;
			Head.verstamp = 0;
			Head.acctype = MainGlobalModule.ConstantsCollection.get("DefAccType");
			Head.firms_id = MainGlobalModule.ConstantsCollection.get("DefFirm");					
			Head.user_id = MainGlobalModule.SettingsCollection.get("ManagerId");
			Head.employees_id = MainGlobalModule.SettingsCollection.get("ManagerId");
			Head.customers_id = null;		
			Head.planned = 0;
			Head.total_paymentsum = 0;			
			
			InsertHeaderIntoDB ();
			
			return true;
			
		} catch (Exception e) {
			
			return false;
			
		}	
		
	}

	private boolean InsertHeaderIntoDB () {
		
		try {
			ContentValues InsertValues = new ContentValues ();
			InsertValues.put("id", Head.id);
			InsertValues.put("docnum", Head.docnum);		
			InsertValues.put("date_create", MainGlobalModule.DateToSqlString(Head.date_create));
			InsertValues.put("date_save", MainGlobalModule.DateToSqlString(Head.date_save));					
			InsertValues.put("state", Head.state);
			InsertValues.put("verstamp", Head.verstamp);
			InsertValues.put("acctype", Head.acctype);
			InsertValues.put("firms_id", Head.firms_id);			
			InsertValues.put("user_id", Head.user_id);
			InsertValues.put("employees_id", Head.employees_id);
			InsertValues.put("customers_id", Head.customers_id);
			InsertValues.put("planned", Head.planned);
			InsertValues.put("total_paymentsum", Head.total_paymentsum);			
			MainGlobalModule.DBE.insert("xdoc_Payments_h", null, InsertValues);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}
	
	public boolean DeleteDoc () {
		
		try {
			
			Head.state = 3;
			UpdateDoc ();
			
			return true;
			
		} catch (Exception e) {
			
			return false;
			
		}	
		
	}
	
	public void FinishDoc () {
		
		if (Head.customers_id==null){
		
			MainGlobalModule.DBE.beginTransaction();
			
				MainGlobalModule.DBE.delete("xdoc_payments_h", "id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_payments_t", "hdoc_id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_payments_d", "hdoc_id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_gpslog", "id=?", new String []{Head.id});
				
			MainGlobalModule.DBE.setTransactionSuccessful();
			MainGlobalModule.DBE.endTransaction();
			
			
		}
		
	}

	public boolean UpdateDoc () {
		
			ContentValues UpdateValues = new ContentValues ();
			UpdateValues.put("id", Head.id);
			UpdateValues.put("docnum", Head.docnum);		
			UpdateValues.put("date_create", MainGlobalModule.DateToSqlString(Head.date_create));
			UpdateValues.put("date_save", MainGlobalModule.DateToSqlString(Calendar.getInstance().getTime()));					
			UpdateValues.put("state", Head.state);
			UpdateValues.put("verstamp", Head.verstamp);
			UpdateValues.put("acctype", Head.acctype);
			UpdateValues.put("firms_id", Head.firms_id);			
			UpdateValues.put("user_id", Head.user_id);
			UpdateValues.put("employees_id", Head.employees_id);
			UpdateValues.put("customers_id", Head.customers_id);
			UpdateValues.put("planned", Head.planned);
			UpdateValues.put("total_Paymentsum", Head.total_paymentsum);
			
			MainGlobalModule.DBE.update("xdoc_Payments_h", UpdateValues, "id=?",new String[] {Head.id} );
			
			return true;
		
	}

	public boolean UpdateDocLine () {
			
		try {			
		
		Line.hdoc_id = Head.id;
		
		Cursor LineIdScalarResult = MainGlobalModule.DBE.rawQuery("select id from xdoc_Payments_t where hdoc_id = ? and ddoc_id = ?",new String [] {Line.hdoc_id,Line.ddoc_id});		
		
		if (LineIdScalarResult.getCount() == 0) {
			Line.id = UUID.randomUUID().toString().toUpperCase();
		}
		else
		{
			LineIdScalarResult.moveToFirst();
			Line.id = LineIdScalarResult.getString(LineIdScalarResult.getColumnIndex("id"));
		}		
			
		ContentValues InsertUpdateValues = new ContentValues ();
		InsertUpdateValues.put("id", Line.id);
		InsertUpdateValues.put("hdoc_id", Line.hdoc_id);
		InsertUpdateValues.put("ddoc_id", Line.ddoc_id);
		InsertUpdateValues.put("dtypedoc_id", Line.dtypedoc_id);
		InsertUpdateValues.put("dtypedoc_descr", Line.dtypedoc_descr);
		InsertUpdateValues.put("ddocnum", Line.ddocnum);
		InsertUpdateValues.put("ddocdate", MainGlobalModule.DateToSqlString(Line.ddocdate));
		InsertUpdateValues.put("dorder_sum", Line.ddorder_sum);
		InsertUpdateValues.put("payment_sum", Line.payment_sum);
			
		if (LineIdScalarResult.getCount() == 0) {
			MainGlobalModule.DBE.insert("xdoc_Payments_t", null, InsertUpdateValues);
			}
		else {
			MainGlobalModule.DBE.update("xdoc_Payments_t", InsertUpdateValues, "id=?",new String[] {Line.id} );
			}
		
		CalculateHeadSums ();
		
		return (true);
		
		} catch (Exception e) {
			
			return (false);
			
		}	
		
		
	}
	
	public boolean DeleteDocLine () {
		
		MainGlobalModule.DBE.delete("xdoc_payments_t", "id = ?", new String []{Line.id});
		CalculateHeadSums ();
		return (true);
		
	}
	
	private void CalculateHeadSums () {
		
		Head.state = 1;
		
		Cursor qSum = MainGlobalModule.DBE.query("xdoc_Payments_t", new String [] {"sum (Payment_sum)"}, "hdoc_id=?", new String [] {Head.id}, null, null, null);
		qSum.moveToFirst();   
		
		Head.total_paymentsum = qSum.getDouble(0);
		
		UpdateDoc ();
				
		
	}

}
